package com.yc.navy.springmvc.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.springmvc.bean.AddrInfo;
import com.yc.navy.springmvc.enums.ResultEnum;
import com.yc.navy.springmvc.service.IAddrInfoService;
import com.yc.navy.springmvc.vo.ResultVO;

@RestController
@RequestMapping("/addr")
public class AddrInfoController {
	@Autowired
	private IAddrInfoService addrInfoService;
	
	@PostMapping("/add")
	public ResultVO add(HttpSession session, AddrInfo af) {
		Integer mno = MemberInfoController.getMno(session);
		if( mno == null ) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		String ano = UUID.randomUUID().toString();
		af.setAno(ano);
		af.setMno(mno);
		int result = addrInfoService.add(af);
		if(result == -100) {
			return new ResultVO(ResultEnum.CHECK_ERROR);
		}
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS, ano);
		}
		return new ResultVO(ResultEnum.ERROP);		
	}
	
	@GetMapping("/list")
	public ResultVO list(HttpSession session) {
		Integer mno = MemberInfoController.getMno(session);
		if( mno == null ) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		List<AddrInfo> list = addrInfoService.list(mno);
		if( list == null || list.isEmpty()) {
			return new ResultVO(ResultEnum.DATA_NULL);
		}
		return new ResultVO(ResultEnum.SUCCESS, list);
		
	}
}
